﻿using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls.Primitives;
using eLearning.Controls;
using eLearning.Model;
using eLearning.Util;
using Microsoft.Phone.Controls;

namespace eLearning
{
    public partial class MainPage : PhoneApplicationPage
    {
        private Popup popup;
        private BackgroundWorker backroungWorker;

        public MainPage()
        {
            InitializeComponent();
            ShowSplash();
        }

        //splash screen
        private void ShowSplash()
        {
            this.popup = new Popup();
            this.popup.Child = new SplashScreenControl();
            this.popup.IsOpen = true;
            StartLoadingData();
        }

        private void StartLoadingData()
        {
            backroungWorker = new BackgroundWorker();
            backroungWorker.DoWork += new DoWorkEventHandler(backroungWorker_DoWork);
            backroungWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backroungWorker_RunWorkerCompleted);
            backroungWorker.RunWorkerAsync();
        }

        private void backroungWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.Dispatcher.BeginInvoke(() =>
            {
                this.popup.IsOpen = false;

                User user = SavedUser.Instance.LoadUser();

                if (user != null && !string.IsNullOrEmpty(user.Password))
                {
                    if (user.Type == User.UserType.Mahasiswa)
                        Utility.GotoPage(this, "/View/Mahasiswa/DashboardPage.xaml");
                    else if (user.Type == User.UserType.Dosen || user.Type == User.UserType.Admin)
                        Utility.GotoPage(this, "/View/Dosen/DashboardPage.xaml");
                }
                else
                {
                    Utility.GotoPage(this, "/View/LoginPage.xaml");
                }
            });
        }

        private void backroungWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            System.Threading.Thread.Sleep(1000);

            Uri uri = new Uri(Setting.Instance.LoadSetting(Config.ROOT_URL_KEY) + "index.php?action=test", UriKind.Absolute);
            WebClient webClient = new WebClient();
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_UploadStringCompleted);
            webClient.DownloadStringAsync(uri);
        }

        void webClient_UploadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                this.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(e.Error.Message);
                });
            else if (e.Result.Contains("Error"))
                this.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show(e.Result);
                });
        }


        //end splash screen
    }
}